﻿Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Text

Public Class Utility

	Public Shared Function SplitDataTable(ByVal dt As DataTable, ByVal noOfRowsInTable As Integer) As DataSet
		Dim ds As New DataSet()

		If dt IsNot Nothing Then

			Dim countSet As Integer = 0
			Dim curCountSet As Integer = 0

			For i As Integer = 0 To dt.Rows.Count - 1

				If i Mod noOfRowsInTable = 0 Then
					ds.Tables.Add(dt.Clone())
					ds.Tables(countSet).TableName = "Set" & (countSet + 1).ToString
					curCountSet = countSet
					countSet += 1
				End If

				Dim dr As DataRow = ds.Tables(curCountSet).NewRow()
				For k As Integer = 0 To dt.Columns.Count - 1
					dr(k) = dt.Rows(i)(k)
				Next
				ds.Tables(curCountSet).Rows.Add(dr)

			Next

			Return ds
		Else
			Return ds
		End If
	End Function

	Public Shared Sub ExportToSpreadsheet(ByVal gv As GridView, ByVal data As DataTable, ByVal name As String)
		Dim context As HttpContext = HttpContext.Current
		context.Response.ClearContent()
		context.Response.Clear()
		context.Response.AddHeader("content-disposition", "attachment;filename=" & name & ".xls")
		context.Response.Charset = ""
		'// If you want the option to open the Excel file without saving than
		'// comment out the line below
		'// Response.Cache.SetCacheability(HttpCacheability.NoCache);
		context.Response.ContentType = "application/vnd.xls"
		Dim stringWrite As New System.IO.StringWriter()
		Dim htmlWrite As New HtmlTextWriter(stringWrite)
		''make temp grid
		Dim gvTemp As New GridView
		gvTemp.AutoGenerateColumns = gv.AutoGenerateColumns
		For Each col As DataControlField In gv.Columns
            gvTemp.Columns.Add(col)
        Next
		gvTemp.DataSource = data
		gvTemp.DataBind()
		gvTemp.RenderControl(htmlWrite)
		gvTemp.Dispose()
		context.Response.Write(stringWrite.ToString())
		context.Response.End()
	End Sub

	Public Shared ReadOnly Property ConnectString As String
		Get
			Try
				Return SS.ApplicationBlock.Config.GetWebConfig("ConnectString")
			Catch
				Return SS.ApplicationBlock.Config.GetAppConfig("ConnectString")
			End Try
		End Get
	End Property

	Public Shared ReadOnly Property Schema As String
		Get
			Try
				Return SS.ApplicationBlock.Config.GetWebConfig("Schema")
			Catch
				Return SS.ApplicationBlock.Config.GetAppConfig("Schema")
			End Try
		End Get
	End Property

	Public Shared Sub SelectChoice(ByVal rbl As RadioButtonList, ByVal value As String)
		UnlockSelected(rbl)
		Dim item As ListItem = rbl.Items.FindByValue(value)
		If item IsNot Nothing Then item.Selected = True
	End Sub

	Public Shared Sub SelectChoice(ByVal ddl As DropDownList, ByVal value As String)
		UnlockSelected(ddl)
		Dim item As ListItem = ddl.Items.FindByValue(value)
		If item IsNot Nothing Then item.Selected = True
	End Sub

	Public Shared Sub SelectChoiceByText(ByVal ddl As DropDownList, ByVal value As String)
		UnlockSelected(ddl)
		Dim item As ListItem = ddl.Items.FindByText(value)
		If item IsNot Nothing Then item.Selected = True
	End Sub

	Public Shared Sub UnlockSelected(ByVal ddl As System.Web.UI.WebControls.DropDownList)
		If ddl.SelectedItem IsNot Nothing Then
			ddl.SelectedIndex = -1
		End If
	End Sub

	Public Shared Sub UnlockSelected(ByVal rbl As System.Web.UI.WebControls.RadioButtonList)
		If rbl.SelectedItem IsNot Nothing Then
			rbl.SelectedIndex = -1
		End If
	End Sub

	Public Shared ReadOnly Property CulInfo As Globalization.CultureInfo
		Get
			Return New System.Globalization.CultureInfo("en-GB")
		End Get
	End Property

	Public Shared Sub SendMail(ByVal fromMail As String, ByVal toMail As String, ByVal ccMail As String, ByVal body As String, ByVal subject As String)
		Dim message As New System.Net.Mail.MailMessage()
		Dim smtpClient As New System.Net.Mail.SmtpClient()
		message.BodyEncoding = Encoding.GetEncoding("windows-874")
		message.SubjectEncoding = Encoding.GetEncoding("windows-874")
		message.To.Add(toMail)
		If Not String.IsNullOrEmpty(ccMail) Then
			message.CC.Add(ccMail)
		End If
		message.Subject = subject
		message.IsBodyHtml = False
		message.Body = body
		smtpClient.Send(message)
	End Sub

	Public Shared Sub SendMail(ByVal fromMail As String, ByVal toMail As String, ByVal body As String, ByVal subject As String)
		Dim message As New System.Net.Mail.MailMessage()
		Dim smtpClient As New System.Net.Mail.SmtpClient()
		message.BodyEncoding = Encoding.GetEncoding("windows-874")
		message.SubjectEncoding = Encoding.GetEncoding("windows-874")
		message.To.Add(toMail)
		message.Subject = subject
		message.IsBodyHtml = False
		message.Body = body
		smtpClient.Send(message)
	End Sub

	Public Shared Function ConvertStatus(ByVal statusId As String) As String
		Select Case statusId
			Case "WN"
				Return "ปกติ(ยังไม่รักษายอด)"
			Case "NM"
				Return "ปกติ(รักษายอด)"
			Case "WC"
				Return "ระงับชั่วคราว(กรุณาติดต่อบริษัทฯ)"
			Case "EX"
				Return "รอต่ออายุสมาชิก 90 วัน"
			Case "WT"
				Return "รอนำส่งเอกสารการสมัคร"
			Case "EP"
				Return "หมดอายุสมาชิก"
			Case "CE"
				Return "โมฆะ"
			Case Else
				Return "ว่าง"
		End Select
	End Function

	Public Shared Function ConvertType(ByVal statusId As String) As String
		Select Case statusId
			Case "O"
				Return "พนักงานในบริษัท"
			Case "S"
				Return "นักธุรกิจ"
			Case Else
				Return ""
		End Select
	End Function

	Public Shared Function ConvertSex(ByVal SexType As String) As String
		Select Case SexType
			Case "M"
				Return "ชาย"
			Case "F"
				Return "หญิง"
			Case Else
				Return ""
		End Select
	End Function

	Public Shared Function ConvertMarriedStatus(ByVal SexType As String) As String
		Select Case SexType
			Case "S"
				Return "โสด"
			Case "M"
				Return "สมรส"
			Case Else
				Return ""
		End Select
	End Function

    Public Shared Function ConvertOfficerCardType(ByVal CardType As String) As String
        Select Case CardType
            Case "PID" 'Personal ID
                Return "บัตรประชาชน"
            Case "CID" 'Company ID
                Return "บัตรนิติบุคคล"
            Case "FID" 'Foreign ID
                Return "บัตรคนต่างชาติ"
            Case "OTH" 'Other Card
                Return "บัตรอื่นๆ"
            Case Else
                Return ""
        End Select
    End Function

	Public Shared Function FormatAppId(ByVal appId As String) As String
		appId = appId.Trim.Replace("-", "")
		If appId.Length < 10 Then
			Return appId
		Else
			Return appId.Substring(0, 2) & "-" & appId.Substring(2)
		End If
	End Function

End Class
